package com.app.service.impl;

import com.app.common.FinancingTool;
import com.app.common.ScaleTool;
import com.app.mapper.ComRecruitersMapper;
import com.app.mapper.EnterpriseMapper;
import com.app.model.*;
import com.app.service.ComRecruitersService;
import com.app.service.EnterpriseService;
import com.app.service.JobPublishService;
import com.app.util.StringTool;
import com.app.vo.CustomerListVo;
import com.app.vo.EnterpriseVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;


/**
 * @Description:公司实现类
 * @Author: N
 * @Date: 2022年11月24日 10点29分
 */
@Service
public class EnterpriseServiceImpl implements EnterpriseService {

    @Autowired
    EnterpriseMapper enterpriseMapper;
    @Autowired
    ComRecruitersService comRecruitersService;
    @Autowired
    JobPublishService jobPublishService;

    @Override
    public int insert(EnterpriseModel model) {
        return enterpriseMapper.insert(model);
    }

    @Override
    public EnterpriseModel findModelById(int id) {
        QueryWrapper<EnterpriseModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("id", id);
        List<EnterpriseModel> userList = enterpriseMapper.selectList(queryWrapper);
        if (userList.isEmpty()) {
            return null;
        } else {
            return userList.get(0);
        }
    }

    @Override
    public void update(EnterpriseModel model) {
        enterpriseMapper.updateById(model);
    }

    @Override
    public List<EnterpriseModel> findAllByName(String comName) {
        QueryWrapper<EnterpriseModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "enterprise_name");
        if (StringTool.isNotEmpty(comName)) {
            queryWrapper.like("enterprise_name", comName);
        }

        List<EnterpriseModel> userList = enterpriseMapper.selectList(queryWrapper);
        if (userList.isEmpty()) {
            return null;
        } else {
            return userList;
        }
    }

    @Override
    public Page<EnterpriseModel> findAllByNamePage(String comName, Integer page, Integer limit) {
        QueryWrapper<EnterpriseModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "enterprise_name", "erterprise_id_num", "street_id");
        if (StringTool.isNotEmpty(comName)) {
            queryWrapper.like("enterprise_name", comName);
        }
        Page<EnterpriseModel> pages = new Page<>();
        pages.setCurrent(page);
        pages.setSize(limit);
        Page<EnterpriseModel> EnterpriseModel = enterpriseMapper.selectPage(pages, queryWrapper);
        if (EnterpriseModel == null) {
            return null;
        } else {
            return EnterpriseModel;
        }
    }

    @Override
    public List<EnterpriseModel> findModelByName(String comName) {
        QueryWrapper<EnterpriseModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "enterprise_name", "logo_url").like("enterprise_name", comName);
        List<EnterpriseModel> userList = enterpriseMapper.selectList(queryWrapper);
        if (userList.isEmpty()) {
            return null;
        } else {
            return userList;
        }
    }

    @Override
    public List<EnterpriseModel> findModelAll(EnterpriseModel model) {
       List<EnterpriseModel> list = enterpriseMapper.getComPopular(model);
       return list;
    }

    @Override
    public EnterpriseModel findLogoById(int id) {
        QueryWrapper<EnterpriseModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "enterprise_name", "logo_url").eq("id", id);
        List<EnterpriseModel> modelList = enterpriseMapper.selectList(queryWrapper);
        if (modelList == null) {
            return null;
        } else {
            return modelList.get(0);
        }
    }

    @Override
    public Page<EnterpriseModel> getModelList(EnterpriseModel model) {
        QueryWrapper<EnterpriseModel> queryWrapper = new QueryWrapper<>();

        if (model.getScaleId() != null && model.getScaleId() > 0) {
            queryWrapper.eq("scale_id", model.getScaleId());
        }
        if (model.getState() != null && model.getState() > 0) {
            queryWrapper.eq("state", model.getState());
        }
        if (StringTool.isNotEmpty(model.getKeyWord())) {
            queryWrapper.and(wrapper -> wrapper.like("enterprise_name", model.getKeyWord()).or().like("legal_person_name", model.getKeyWord()));
        }
        queryWrapper.orderByDesc("crt_time");
        Page<EnterpriseModel> page = new Page<>();
        page.setCurrent(model.getPage());
        page.setSize(model.getLimit());
        Page<EnterpriseModel> EnterpriseModel = enterpriseMapper.selectPage(page, queryWrapper);
        if (EnterpriseModel == null) {
            return null;
        } else {
            return EnterpriseModel;
        }
    }

    @Override
    public int getModelCount(EnterpriseModel model) {
        QueryWrapper<EnterpriseModel> queryWrapper = new QueryWrapper<>();
        if (model != null && model.getState() != null) {
            queryWrapper.eq("state", model.getState());
        }
        int count = enterpriseMapper.selectCount(queryWrapper);
        return count;
    }

    @Override
    public EnterpriseVo getCompanyList(EnterpriseModel model) {
        Page<EnterpriseModel> modelPage = getModelList(model);
        List<EnterpriseModel> modelList = modelPage.getRecords();
        for (int i = 0; i < modelList.size(); i++) {
            modelList.get(i).setHrCount(comRecruitersService.getCountByComId(modelList.get(i).getId()));
            modelList.get(i).setJobPublishCount(jobPublishService.getJobPublishCountByComId(modelList.get(i).getId()));
            if (modelList.get(i).getScaleId() != null) {
                for (int j = 0; j < ScaleTool.SCALE_VO_LIST.size(); j++) {
                    if (modelList.get(i).getScaleId() == ScaleTool.SCALE_VO_LIST.get(j).getId()) {
                        modelList.get(i).setScaleName(ScaleTool.SCALE_VO_LIST.get(j).getData());
                    }

                }
            }

            for (int k = 0; k < FinancingTool.FINANCING_VO_LIST.size(); k++) {
                if (modelList.get(i).getFinancingId() != null && modelList.get(i).getFinancingId() == FinancingTool.FINANCING_VO_LIST.get(k).getId()) {
                    modelList.get(i).setFinancingName(FinancingTool.FINANCING_VO_LIST.get(k).getName());
                }
            }
        }
        int count = (int) modelPage.getTotal();
        EnterpriseVo vo = new EnterpriseVo();
        vo.setCount(count);
        vo.setList(modelList);
        return vo;
    }

    @Override
    public List<EnterpriseModel> findModelByIds(String[] comIds) {
        QueryWrapper<EnterpriseModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "enterprise_name", "logo_url").in("id", comIds);
        return enterpriseMapper.selectList(queryWrapper);
    }

    @Override
    public Integer selectEmployersCount() {
        return enterpriseMapper.selectCount(null);
    }

    @Override
    public List<EnterpriseModel> selectAll() {
        return enterpriseMapper.selectList(null);
    }

    @Override
    public Page<EnterpriseModel> employersList(Integer page1, Integer limit) {
        QueryWrapper<EnterpriseModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("crt_time");
        Page<EnterpriseModel> page = new Page<>();
        page.setCurrent(page1);
        page.setSize(limit);
        Page<EnterpriseModel> EnterpriseModel = enterpriseMapper.selectPage(page, queryWrapper);
        if (EnterpriseModel == null) {
            return null;
        } else {
            return EnterpriseModel;
        }
    }

    @Override
    public List<EnterpriseModel> enterprise(Integer page, Integer limit, Integer streetId) {

        return null;
    }

    @Override
    public List<EnterpriseModel> getComByStreet(String startTime, String endTime) {
        return enterpriseMapper.getComByStreet(startTime, endTime);
    }

    @Override
    public List<EnterpriseModel> getComAndStreet(String startTime, String endTime) {
        List<EnterpriseModel> list = enterpriseMapper.getComAndStreet(startTime, endTime);
        return list;
    }

    @Override
    public EnterpriseVo getComPageByStreetId(EnterpriseModel model) {

        QueryWrapper<EnterpriseModel> queryWrapper = new QueryWrapper<>();
        if(model.getStreetId()!=null&&model.getStreetId()>0)
        {
            queryWrapper.eq("street_id", model.getStreetId());
        }
        if(StringTool.isNotEmpty(model.getStartTime()))
        {
            queryWrapper.apply(" TO_DAYS(crt_time) >= TO_DAYS('"+model.getStartTime()+"')");
        }
        if(StringTool.isNotEmpty(model.getEndTime()))
        {
            queryWrapper.apply(" TO_DAYS(crt_time) <= TO_DAYS('"+model.getEndTime()+"')");
        }
        Page<EnterpriseModel> pages = new Page<>();
        pages.setCurrent(model.getPage());
        pages.setSize(model.getLimit());
        Page<EnterpriseModel> page = enterpriseMapper.selectPage(pages, queryWrapper);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (page == null) {
            return null;
        } else {
            EnterpriseVo vo = new EnterpriseVo();
            vo.setList(page.getRecords());
            vo.setCount((int) page.getTotal());
            return vo;
        }
    }
    @Override
    public Page<EnterpriseModel> getKeynotePage(Integer page, Integer limit) {
        QueryWrapper<EnterpriseModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("enterprise_name","酒泉恒瑞新石英材料有限公司","甘肃圣锦元生物科技有限公司","甘肃松盛医药有限公司","瓜州县强力商砼有限公司","瓜州县伟莹建材有限公司","甘肃三新硅业有限公司","瓜州县热力有限公司","甘肃胜安矿业有限公司","瓜州县宝海矿业有限公司","瓜州县晋恒矿业有限公司","甘肃省瓜州县腾飞矿业有限责任公司","柳园古堡泉金属精选有限责任公司","瓜州县鑫港矿业开发有限责任公司","西脉黄金股份有限公司","甘肃瓜州柳园交通石业有限公司","瓜州县豪东贸易有限公司","瓜州县双鑫矿业有限责任公司","甘肃省鑫源能源有限公司","酒泉鲁冠化工科技有限公司","瓜州县弘也水泥有限责任公司","瓜州高杨建材有限公司","瓜州德泰电气化铁路物资有限公司","瓜州县鸿建商品混凝土有限公司","甘肃中科凯越硅材料科技有限公司","瓜州县三元冶炼有限责任公司","瓜州县新哈矿业开发有限责任公司","酒钢（集团）瓜州长城电力机械工程有限公司","甘肃瓜州瑞尔力风电装备制造有限公司","甘肃中科泰信新能源装备有限公司","甘肃科耀电力有限公司","酒泉新能钢构有限公司","甘肃鑫汇风电开发有限责任公司","甘肃酒泉汇能风电开发有限责任公司","中国水电顾问集团风电瓜州有限公司","国投酒泉第二风电有限公司","龙源（酒泉）风力发电有限公司","瓜州县润浩新能源有限公司","国电电力瓜州风力发电有限公司","国投酒泉第一风电有限公司","甘肃华电环县风力发电有限公司瓜州分公司","甘肃新泉风力发电有限公司","中国水电建设集团瓜州风电有限公司","国电甘肃新能源有限公司","华能酒泉风电有限责任公司","大唐瓜州新能源有限公司","甘肃电投鼎新风电有限责任公司","甘肃汇能安北风电有限公司","酒泉泰源新能源有限公司","瓜州泰合风力发电有限公司","亚洲新能源电力（瓜州）有限公司","京城瓜州新能源有限公司","中电建瓜州新能源有限公司","瓜州新盛安北风力发电有限公司","国投酒泉新能源有限公司","酒泉盾安新能源有限公司","瓜州县风润风电有限公司","瓜州县光源光伏发电有限公司","三峡新能源酒泉有限公司","瓜州中能峰频电力有限公司","甘肃电投常乐发电有限责任公司","甘肃龙源新能源有限公司","甘肃新安风力发电有限公司","甘肃瓜州协合风力发电有限公司","瓜州天润风电有限公司","华润电力风能（瓜州）有限公司","甘肃中电酒泉第三风力发电有限公司","甘肃中广核风力发电有限公司","中广核甘肃瓜州风力发电有限公司","甘肃中广核永盛风力发电有限公司","甘肃中电瓜州风力发电有限公司","中广核甘肃瓜州第二风力发电有限公司","寰泰工业(瓜州)有限公司","甘肃骐鹏生物科技有限公司","甘肃新海鹏化工科技有限公司","瓜州县超迈建材有限公司","华能瓜州能源有限公司","甘肃军垦商砼有限公司","瓜州国能光伏发电有限公司");
        queryWrapper.orderByDesc("crt_time");
        Page<EnterpriseModel> enterpriseModelPage = new Page<>();
        enterpriseModelPage.setCurrent(page);
        enterpriseModelPage.setSize(limit);
        Page<EnterpriseModel> EnterpriseModel = enterpriseMapper.selectPage(enterpriseModelPage, queryWrapper);
        if (EnterpriseModel == null) {
            return null;
        } else {
            return EnterpriseModel;
        }
    }

}
